package com.yx.cloud.map.layer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.graphics.PointF;
import android.view.MotionEvent;

import com.aaa.lib.map.MapUtils;
import com.aaa.lib.map.MapView;
import com.aaa.lib.map.area.LineArea;
import com.aaa.lib.map.layer.AreaLayer;
import com.yx.cloud.R;
import com.yx.cloud.map.YXMapView;
import com.iot.product.sweeper.bean.MapInfo;
import com.iot.common.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class YXAreaDivideLineLayer extends AreaLayer {
    public static final String TAG = "YXAreaDivideLineLayer";
    protected float[] tmpPoint;
    protected volatile PointF p3, p4; //交点
    protected PointF p1, p2; //分割线两个端点
    protected int pointRadus = 0; //分割线端点圆的半径 也是点击范围的半径
    //是否点击两个端点 做拖拽用
    protected boolean isTouchP1 = false;
    protected boolean isTouchP2 = false;
    protected boolean isTouchLine = false;
    protected Paint mPaint; //画笔
    protected PathEffect mDashPath; //虚线效果
    protected List<PointF> crossPoints;
    protected float lastPosX;
    protected float lastPosY;
    protected OnDividerMoveListener onDividerMoveListener;


    public YXAreaDivideLineLayer(MapView mapView) {
        super(mapView);
        p1 = new PointF(-1, -1);
        p2 = new PointF(-1, -1);
        area = new LineArea();

        crossPoints = new ArrayList<>();

        tmpPoint = new float[2];

        //通过屏幕密度设置具体 线宽度 端点半径
        Context context = mapView.getContext().getApplicationContext();
        float scale = context.getResources().getDisplayMetrics().density;
        int lineWidth = (int) (2 * scale);
        pointRadus = (int) (9 * scale);
        LogUtils.i("YXAreaDivideLineLayer screen density： " + scale);

        //设置画笔
        mDashPath = new DashPathEffect(new float[]{3, 3}, 0);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeWidth(lineWidth);
        mPaint.setColor(context.getResources().getColor(R.color.area_divide_line));
    }

    public void setLine(PointF start, PointF end) {
        setLine(start.x, start.y, end.x, end.y);
    }

    public void setLine(float startX, float startY, float endX, float endY) {
        p1.x = startX;
        p1.y = startY;
        p2.x = endX;
        p2.y = endY;

        if(onDividerMoveListener!=null){
            onDividerMoveListener.onDividerMove(startX,startY,endX,endY);
        }

    }

    public void setDividerMoveListener(OnDividerMoveListener listener) {
        onDividerMoveListener = listener;
    }

    @Override
    public boolean onTouch(MotionEvent event) {
        return handleTouch(event);
    }

    protected boolean handleTouch(MotionEvent event) {
        LogUtils.i(" x : " + event.getX() + "y " + event.getY());
        //获取反转矩阵 ，用于获取坐标平移前的点
        MapUtils.getInverseRotatedPoint(mMapView.getTransform(), event.getX(), event.getY(), tmpPoint);
        float x = tmpPoint[0];
        float y = tmpPoint[1];
        LogUtils.i(" x : " + x + "y " + y);

        float scale = ((YXMapView)mMapView).getScale();

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //判断是否点击端点 准备拖动
            if (MapUtils.isInCircle(x, y, p1.x, p1.y, pointRadus * 3 / scale)) {
                isTouchP1 = true;
            } else if (MapUtils.isInCircle(x, y, p2.x, p2.y, pointRadus * 3 / scale)) {
                isTouchP2 = true;
            } else if (MapUtils.pointToLine(x, y, p1.x, p1.y, p2.x, p2.y) < pointRadus * 3 / scale) {
                isTouchLine = true;
                lastPosX = x;
                lastPosY = y;
            } else {
                return false;
            }
            setCrossPoints(null,null);
            return true;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            LogUtils.i("move to x: " + x + " y : " + y);
            //将端点移到拖动位置
            if (isTouchP1) {
                movePoint(p1, x, y);
            } else if (isTouchP2) {
                movePoint(p2, x, y);
            } else if (isTouchLine) {
                float tempX = x - lastPosX;
                float tempY = y - lastPosY;
                movePoint(p1, p1.x + tempX, p1.y + tempY);
                movePoint(p2, p2.x + tempX, p2.y + tempY);
                lastPosX = x;
                lastPosY = y;
            } else {
                return false;
            }
            mMapView.refresh();

            return true;

        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (isTouchP1 || isTouchP2 || isTouchLine) {
                isTouchP1 = false;
                isTouchP2 = false;
                isTouchLine =false;
                refreshDivider();
                return true;
            }
        }
        return false;
    }
    public void refreshDivider(){
        if (onDividerMoveListener != null) {
            onDividerMoveListener.onDividerMove(p1.x, p1.y, p2.x, p2.y);
        }
    }

    protected void movePoint(PointF point, float x, float y) {
        point.x = x;
        point.y = y;
    }

    @Override
    public void draw(Canvas canvas) {
        float scale = ((YXMapView)mMapView).getScale();

        canvas.save();

        //清除虚线效果
        canvas.setMatrix(mMapView.getTransform());
        //设置虚线效果 画虚线
        mPaint.setStrokeWidth(5 / scale);
        mPaint.setPathEffect(mDashPath);
        canvas.drawLine(p1.x, p1.y, p2.x, p2.y, mPaint);

        mPaint.setPathEffect(null);
        if (p3 != null && p4 != null) {
            canvas.drawLine(p3.x, p3.y, p4.x, p4.y, mPaint);
        }

        //画两个端点的圆
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(p1.x, p1.y, pointRadus / scale, mPaint);
        canvas.drawCircle(p2.x, p2.y, pointRadus / scale, mPaint);
        canvas.restore();
    }

    @Override
    public void release() {

    }

    public void setCrossPoints(PointF c1, PointF c2) {
        p3 = c1;
        p4 = c2;
        crossPoints.clear();
        if(c1!=null&&c2!=null){
            crossPoints.add(p3);
            crossPoints.add(p4);
        }
        mMapView.refresh();
    }
    public List<PointF> getCrossPoint() {
        return crossPoints;
    }


    public static List<PointF> calculateCrossPoint(MapInfo mapInfo, int roomId, double x1, double y1, double x2, double y2) {
        int[] mapData = mapInfo.mapData;

        double distance = MapUtils.distance(x1, y1, x2, y2);
        double stepX = (x2 - x1) / distance;
        double stepY = (y2 - y1) / distance;
        List<PointF> pointList = new ArrayList<>();
        for (int i = 0; i < distance; i++) {
            double x =(x1 + i * stepX);
            double y =  (y1 + i * stepY);
            LogUtils.i("line point i" + i + " (" + x + "," + y + ")");
            if (x < 0 || x >= mapInfo.width || y < 0 || y >= mapInfo.height) {
                continue;
            }
            LogUtils.i("line point i" +  mapData[(int)y * mapInfo.width + (int)x]);
            if (roomId == mapData[(int)y * mapInfo.width + (int)x]) {
                pointList.add(new PointF((float) x, (float)y));
            }
        }
        return pointList;
    }

    public interface OnDividerMoveListener {
        void onDividerMove(float x1, float y1, float x2, float y2);
    }


}
